home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Interapplication Comm / 7Edit / Source / SVAECut.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.4 KB  |  148 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SVAECut.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Original version by Jon Lansdell and Nigel Humphreys.
  7.                 3.1 updates by Greg Sutton.
  8.  
  9.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #include "SVAECut.h"
  25.  
  26. #include "SVEditAEUtils.h"
  27. #include "SVEditWindow.h"        // for DPtrFromWindowPtr()
  28.  
  29. #include "SVAESelect.h"
  30.  
  31. #include <Scrap.h>
  32.  
  33.  
  34. #pragma segment AppleEvent
  35.  
  36. // Handle a cut to scrap e.g 'copy last word of document 1'
  37. // If no reference is given then the selection of the front window
  38. // is used.
  39.      
  40. pascal OSErr    DoCut(const AppleEvent *theAppleEvent, AppleEvent *reply, long refcon)
  41. {
  42. #pragma unused (reply, refcon)
  43.  
  44.     AEDesc        directObj = {typeNull, NULL};
  45.     TextToken    aTextToken;
  46.     short        ignore;
  47.     OSErr        err;
  48.  
  49.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &directObj);
  50.     // If we get an error here it just means that they haven't supplied a reference to
  51.     // an object to cut - so cut the current section instead.
  52.     
  53.     if (directObj.descriptorType != typeNull)
  54.         err = CutDesc(&directObj);
  55.     else
  56.     {            // Just cut the selection of the front window
  57.         err = GetWindowSelection(FrontWindow(), &aTextToken, &ignore);
  58.         if (noErr != err) goto done;
  59.         
  60.         err = CutTextToken(&aTextToken);
  61.     }
  62.  
  63. done:    
  64.     if (directObj.dataHandle)
  65.         AEDisposeDesc(&directObj);
  66.         
  67.     return(err);
  68. } // DoCut
  69.  
  70.  
  71. OSErr    CutTextToken(TextToken* theToken)
  72. {
  73.     WindowPtr        aWindow;
  74.     DPtr            docPtr;
  75.     OSErr            err;
  76.     
  77.     aWindow = theToken->tokenWindow;
  78.     docPtr = DPtrFromWindowPtr(theToken->tokenWindow);
  79.     
  80.     if (! aWindow || ! docPtr)
  81.         return(errAENoSuchObject);
  82.  
  83.                     // Set this tokens selection
  84.     err = SelectTextToken(theToken);
  85.     if (noErr != err) goto done;
  86.  
  87.     err = (OSErr)ZeroScrap();
  88.     TECut(docPtr->theText);     
  89.             
  90.     docPtr->dirty = true;
  91.     AdjustScrollbars(docPtr, false);
  92.     DrawPageExtras(docPtr);
  93.     
  94. done:
  95.     return(err);
  96. }
  97.  
  98. OSErr    CutTextDesc(AEDesc* textDesc)
  99. {
  100.     TextToken        aTextToken;
  101.     Size            actualSize;
  102.     OSErr            err;
  103.  
  104.     if (typeMyText != textDesc->descriptorType)
  105.         return(errAETypeError);
  106.         
  107.     GetRawDataFromDescriptor(textDesc, (Ptr)&aTextToken, sizeof(aTextToken), &actualSize);
  108.  
  109.     err = CutTextToken(&aTextToken);
  110.     
  111.     return(err);
  112. }
  113.  
  114. OSErr    CutDesc(AEDesc* aDesc)
  115. {
  116.     AEDesc        cutDesc = {typeNull, NULL},
  117.                 textDesc = {typeNull, NULL};
  118.     OSErr        err;
  119.     
  120.     if (typeObjectSpecifier == aDesc->descriptorType)
  121.         err = AEResolve(aDesc, kAEIDoMinimum, &cutDesc);
  122.     else
  123.         err = AEDuplicateDesc(aDesc, &cutDesc);
  124.         
  125.     if (noErr != err) goto done;
  126.     
  127.     switch (cutDesc.descriptorType)
  128.     {
  129.         case typeAEList:
  130.             err = errAETypeError;
  131.             // We can't handle cutting more than one item to the scrap
  132.             break;
  133.             
  134.         default:
  135.             err = AECoerceDesc(&cutDesc, typeMyText, &textDesc);
  136.             if (noErr != err) goto done;
  137.             err = CutTextDesc(&textDesc);
  138.     }
  139.     
  140. done:
  141.     if (cutDesc.dataHandle)
  142.         AEDisposeDesc(&cutDesc);
  143.     if (textDesc.dataHandle)
  144.         AEDisposeDesc(&textDesc);
  145.     
  146.     return(err);
  147. }
  148.